//
//  TextViewController.m
//  Novels
//
//  Created by iajmani on 2/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TextViewController.h"




@implementation TextViewController

@synthesize  myTextView;
@synthesize  books;
//- (void) loadText :(NSString *) path
//{//important
//	NSString	*textString=[NSString stringWithContentsOfFile: path encoding:NSUTF8StringEncoding error: NULL];
//	self.myTextView.text=textString; //set loading text
//	
//}

- (void) loadText :(NSString *) textString
{//important
	self.myTextView.text=textString; //set loading text
		
	
}

//Xin Shu: the reaction method after click the rightbarbutton 
-(void) clickRightButtonHappened{		 
	NSLog(self.myTextView.text);
	NSString *documentDirectoryPath = [Utilities getFilename];
	
	//Xin Shu: determine which data to save
	
	NSData *dataToWrite = 
	[NSPropertyListSerialization dataWithPropertyList:self.myTextView.text
											   format:NSPropertyListXMLFormat_v1_0
											  options:0
												error:nil];
	
	if (dataToWrite != nil) {
		[dataToWrite writeToFile:documentDirectoryPath atomically:NO]; //Xin Shu: store as file
	}
	NSLog(documentDirectoryPath);
}




// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//Xin Shu: this adds the rightbarbutton to the TextViewController and the method when click the button
	self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
																						  target:self 
																						  action:@selector(clickRightButtonHappened)];

	NSLog([[DocSelectionViewController myNoteList] theList]);
	NSString *text=[[[[DocSelectionViewController myNoteList] theList] valueForKey:@"FirstNote"] valueForKey:@"Content"];
	[self loadText:text];
	

}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
